﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using CNVP.Framework.Utils;

namespace CNVP.Framework.Helper
{
    public class LogHelper
    {
        /// <summary>
        /// 系统操作日志
        /// </summary>
        /// <param name="Title">日志标题</param>
        /// <param name="Message">日志内容</param>
        public static void Write(string Title, string Message)
        {
            StreamWriter sw = null;
            DateTime date = DateTime.Now;
            string FileName = DateTime.Now.ToString("yyyy-MM-dd");
            try
            {
                FileName = HttpContext.Current.Server.MapPath("~/App_Logs/") + FileName + ".txt";
                #region 检测日志目录是否存在
                if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/App_Logs")))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/App_Logs"));
                }

                if (!File.Exists(FileName))
                    sw = File.CreateText(FileName);
                else
                {
                    sw = File.AppendText(FileName);
                }
                #endregion
                sw.WriteLine("IP地址：" + Public.GetUserIP() + "\r");
                sw.WriteLine("标  题：" + Title + "\r");
                sw.WriteLine("内  容：" + Message);
                sw.WriteLine("时  间：" + System.DateTime.Now);
                sw.WriteLine("≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\r");
                sw.Flush();
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }
        }
    }
}